package com.jyh.util;


import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtilsBean;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;

public class JavaBeanForMap {
	public static final Logger logger = LoggerFactory.getLogger(JavaBeanForMap.class);
	/**
	 * 将javabean实体类转为map类型，然后返回一个map类型的值
	 * 
	 * @param obj
	 * @return
	 */
	public static Map<String, String> beanToMap(Object obj) {
		Map<String, String> params = new HashMap<String, String>(0);
		try {
			PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
			PropertyDescriptor[] descriptors = propertyUtilsBean.getPropertyDescriptors(obj);
			for (int i = 0; i < descriptors.length; i++) {
				String name = descriptors[i].getName();
				if (!"class".equals(name)) {
					String vault = propertyUtilsBean.getNestedProperty(obj, name) + "";
					if (getMapString(vault)) {
						params.put(name, vault);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return params;
	}

	/**
	 * 将json转化为实体POJO
	 * 
	 * @param jsonStr
	 * @param obj
	 * @return
	 * @throws IOException
	 * @throws JsonMappingException
	 * @throws JsonParseException
	 */
	public static <T> Object jsonToObject(String jsonStr, Class<T> obj)
			throws JsonParseException, JsonMappingException, IOException {
		ObjectMapper objectMapper = new ObjectMapper();
		T t = objectMapper.readValue(jsonStr, obj);
		return t;
	}
	
	/**
	 * 将json转化为实体POJO
	 * 
	 * @param jsonStr
	 * @param obj
	 * @return
	 * @throws IOException
	 * @throws JsonMappingException
	 * @throws JsonParseException
	 */
	public static <T> T JSONToObj(String jsonString, Class<T> pojoCalss) throws IOException {
		net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(jsonString);
		Object pojo = net.sf.json.JSONObject.toBean(jsonObject, pojoCalss);
		logger.info("转换实体参数："+beanToMap(pojo));
		return (T) pojo;
	}
	
    /**
     * 将实体POJO转化为JSON
     * @param obj
     * @return
     * @throws JSONException
     * @throws IOException
     */
    public static<T> String objectToJson(T obj) throws JSONException, IOException {
        ObjectMapper mapper = new ObjectMapper();  
        // Convert object to JSON string  
        String jsonStr = "";
        try {
             jsonStr =  mapper.writeValueAsString(obj);
        } catch (IOException e) {
            throw e;
        }
        return jsonStr;
    }

	/**
	 * 验证参数是否为空
	 * 
	 * @param str
	 * @return
	 */
	public static boolean getMapString(String str) {
		if ("null".equals(str) || "".equals(str) || null == str) {
			str = "false";
		}
		return true;
	}
}
